﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BaoGiaVeThang
{
    public partial class BaoGia : Form
    {
        public VNACrawler[] vNACrawlers = new VNACrawler[5];
        public BLCrawler[] bLCrawlers = new BLCrawler[2];
        public VJCrawler vJCrawler = new VJCrawler(1);
        private bool[] dones = new bool[8];
        public BaoGia()
        {
            InitializeComponent();
        }
        private void showLog(TextBox tShow,AirLineCrawler airLineCrawler)
        {
            tShow.Text = "Tháng :" + airLineCrawler.monthInfo.month + "-- điểm đi :" + airLineCrawler.domesticRoute.DepartureAirport + "--- điểm đến :" + airLineCrawler.domesticRoute.ArrivalAirport 
                + "--Lỗi : " + airLineCrawler.countError + "/" + airLineCrawler.countTotal;
        }
        private void SetAndExitIfAllDone(int index)
        {
            dones[index] = true;
            foreach (bool t in dones)
            {
                if (!t)
                {
                    return;
                }
            }
            foreach (VNACrawler v in vNACrawlers)
                v.SaveLog("VietNamAirLine"+v.monthInfo.startDay);
            vJCrawler.SaveLog("VietJetAir" + vJCrawler.monthInfo.startDay);
            bLCrawlers[0].SaveLog("JetStar" + bLCrawlers[0].monthInfo.startDay);
            bLCrawlers[1].SaveLog("JetStar" + bLCrawlers[1].monthInfo.startDay);
            Application.Exit();
        }
        private void timerVNA1_Tick(object sender, EventArgs e)
        {
            if (vNACrawlers[0].isDone())
            {
                timerVNA1.Stop();
                SetAndExitIfAllDone(0);
            }
            else
            {
                vNACrawlers[0].UpdateCheapTicketPrice();
                showLog(txtVN1, vNACrawlers[0]);
            }
        }

        private void timerVNA2_Tick(object sender, EventArgs e)
        {
            if (vNACrawlers[1].isDone())
            {
                timerVNA2.Stop();
                SetAndExitIfAllDone(1);
            }
            else
            {
                vNACrawlers[1].UpdateCheapTicketPrice();
                showLog(txtVN2, vNACrawlers[1]);
            }
        }

        private void timerVNA3_Tick(object sender, EventArgs e)
        {
            if (vNACrawlers[2].isDone())
            {
                timerVNA3.Stop();
                SetAndExitIfAllDone(2);
            }
            else
            {
                vNACrawlers[2].UpdateCheapTicketPrice();
                showLog(txtVN3, vNACrawlers[2]);
            }
        }

        private void timerVNA4_Tick(object sender, EventArgs e)
        {
            if (vNACrawlers[3].isDone())
            {
                timerVNA4.Stop();
                SetAndExitIfAllDone(3);
            }
            else
            {
                vNACrawlers[3].UpdateCheapTicketPrice();
                showLog(txtVN4, vNACrawlers[3]);
            }
        }

        private void timerVNA5_Tick(object sender, EventArgs e)
        {
            if (vNACrawlers[4].isDone())
            {
                timerVNA5.Stop();
                SetAndExitIfAllDone(4);
            }
            else
            {
                vNACrawlers[4].UpdateCheapTicketPrice();
                showLog(txtVN5, vNACrawlers[4]);
            }
        }

        private void timerVJ_Tick(object sender, EventArgs e)
        {
            if (vJCrawler.isDone())
            {
                timerVJ.Stop();
                SetAndExitIfAllDone(7);
            }
            else
            {
                vJCrawler.UpdateCheapTicketPrice();
                showLog(txtVJ, vJCrawler);
            }
        }

        private void timerBL1_Tick(object sender, EventArgs e)
        {
            if (bLCrawlers[0].isDone())
            {
                timerBL1.Stop();
                SetAndExitIfAllDone(5);
            }
            else
            {
                bLCrawlers[0].UpdateCheapTicketPrice();
                showLog(txtBL1, bLCrawlers[0]);
            }
        }

        private void timerBL2_Tick(object sender, EventArgs e)
        {
            if (bLCrawlers[1].isDone())
            {
                timerBL2.Stop();
                SetAndExitIfAllDone(6);
            }
            else
            {
                bLCrawlers[1].UpdateCheapTicketPrice();
                showLog(txtBL2, bLCrawlers[1]);
            }
        }

        private void BaoGia_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 5; i++)
            {
                vNACrawlers[i] = new VNACrawler(i * 7 + 1);
            }
            for (int i = 0; i < 8; i++)
            {
                dones[i] = false;
            }
            timerVNA1.Start();
            Thread.Sleep(1600);
            timerVNA2.Start();
            Thread.Sleep(1600);
            timerVNA3.Start();
            Thread.Sleep(1600);
            timerVNA4.Start();
            Thread.Sleep(1600);
            timerVNA5.Start();
            bLCrawlers[0] = new BLCrawler(1);
            bLCrawlers[1] = new BLCrawler(21);
            Thread.Sleep(1000);
            timerBL1.Start();//6
            Thread.Sleep(1600);
            timerBL2.Start();//7
            Thread.Sleep(1600);
            timerVJ.Start();//5
            Thread.Sleep(1600);
        }
    }
}
